package com.zhhtech.zhh.ui.activity.main

import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.zhhtech.zhh.R
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.IncomeBean
import com.zhhtech.zhh.databinding.ActivityRefreshBinding
import com.zhhtech.zhh.databinding.ItemWithdrawRecordBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.base.BaseRecyclerAdapter
import com.zhhtech.zhh.ui.ext.dp
import com.zhhtech.zhh.ui.ext.showEmptyCallback
import com.zhhtech.zhh.ui.ext.toColor
import com.zhhtech.zhh.ui.helper.loadsir.registerLoadSir

class IncomeListActivity : BaseActivity<ActivityRefreshBinding>() {
    private lateinit var mLoadService: LoadService<*>
    private val mAdapter = WithdrawRecordAdapter()
    override fun initView() {
        vb.titleBar.titleStr = "收益明细"
        mLoadService = vb.refreshLayout.registerLoadSir(::onReload)
        vb.refreshLayout.setEnableRefresh(false)
        vb.refreshLayout.setEnableLoadMore(false)
        vb.rvList.layoutManager = LinearLayoutManager(mThis)
        vb.rvList.addItemDecoration(DividerItemDecoration(mThis, DividerItemDecoration.VERTICAL))
        vb.rvList.setBackgroundColor(R.color.white.toColor())
        vb.rvList.setPadding(15.dp, 0, 15.dp, 0)
        onReload()
    }

    private fun onReload() {
        launchUI({
            mAdapter.datas = api.incomeList().toData()?.list ?: mutableListOf()
            vb.rvList.adapter = mAdapter
        }, ApiObserver(loadService = mLoadService)) {
            if (mAdapter.datas.isEmpty()) {
                mLoadService.showEmptyCallback()
            }
        }
    }
}

class WithdrawRecordAdapter : BaseRecyclerAdapter<ItemWithdrawRecordBinding, IncomeBean>() {
    override fun onBindViewHolder(vb: ItemWithdrawRecordBinding, bean: IncomeBean, position: Int) {
        vb.tvPhone.text = bean.title
        vb.tvCrateAt.text = bean.createAt
        vb.tvIncome.text = "${if ("1" == bean.inType) "+" else "-"}${bean.amount}"
        setViewClick(position, vb.root)
    }
}